﻿using DevExpress.XtraEditors;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace KoliUstu
{
    public partial class Login : Form
    {
//        private static string path = @"c:\KoliUstu\info.dat";
        private static string path = "info.dat";
        private static ceGate.ceGateSoapClient gate = new ceGate.ceGateSoapClient();

        public Login()
        {
            InitializeComponent();
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Office 2013";
        }


        private void btnGiris_Click(object sender, EventArgs e)
        {
            string password = txtParola.Text;
            DataSet ds = gate.GetCredentials(password, password, "confexpert.karbel.com", "");
            if (ds == null)
            {
                XtraMessageBox.Show("Hatalı giriş!", "Hata", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                txtParola.Text = "";
            }

            else
            {
                File.WriteAllText(path, password);
                this.DialogResult = DialogResult.OK;
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        public static bool DoLogin()
        {
            FileInfo fi = new FileInfo(path);

            if (!File.Exists(path))
            {
                if (!fi.Directory.Exists)
                    Directory.CreateDirectory(fi.DirectoryName);

                File.Create(path);

                return ShowForm();
            }
            else
            {

                string password = File.ReadAllText(fi.FullName);
                if (password == "")
                    return ShowForm();
                else
                {
                    DataSet ds = gate.GetCredentials(password, password, "confexpert.karbel.com", "");
                    if (ds == null)
                        return false;
                    else
                    {
                        string cnn = ds.Tables[0].Rows[0][0].ToString();
                        DevTeam.Session.DataManager = new DevTeam.DataManager(cnn);
                        return true;
                    }
                      
                }
            }
        }

        private static bool ShowForm()
        {
            Login login = new Login();
            return login.ShowDialog() == DialogResult.OK;
        }

        private void txtParola_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnGiris.PerformClick();
        }
    }
}